/**
 * @author xkh
 * @date 2019-12-02 9:04
 */
package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupdao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupdao checkGroupdao;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkItemIds) {
        checkGroupdao.add(checkGroup);
        setCheckGroupAndCheckItem(checkGroup.getId(), checkItemIds);
    }

    //设置检查组合和检查项的关联关系
    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id", checkGroupId);
                map.put("checkitem_id", checkitemId);
                checkGroupdao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page= checkGroupdao.selectByCondition(queryString);
        return new  PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        checkGroupdao.delete(id);
        checkGroupdao.deleteById(id);
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupdao.findById(id);
    }

    @Override
    public List<Integer> findCheckitemIdsBycheckGroupId(Integer id) {
        return checkGroupdao.findCheckitemIdsBycheckGroupId(id);
    }

    @Override
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupdao.edit(checkGroup);
        checkGroupdao.delete(checkGroup.getId());
        setCheckGroupAndCheckItem(checkGroup.getId(), checkitemIds);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupdao.findAll();
    }
}
